home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / examples / sparse < prev    next >
Text File  |  1994-09-20  |  776b  |  52 lines

  1. //
  2. // Create and manipulate sparse matrices via RLaB lists.
  3. //
  4.  
  5. //
  6. // Given row and column indices, create a sparse matrix
  7. // structure using lists.
  8. //
  9.  
  10. make_sparse = function ( cm )
  11. {
  12.   sm = <<>>;  // Create initial list.
  13.  
  14.   ri = set (cm[;1]);  // Get a set of the row indices.
  15.   for (i in 1:ri.n)   // Create the row lists.
  16.   {
  17.     sm.[ri[i]] = <<>>;
  18.   }
  19.  
  20.   // Now load up the sparse matrix
  21.   
  22.   for (i in 1:cm.nr)
  23.   {
  24.     sm.[cm[i;1]].[cm[i;2]] = cm[i;3];
  25.   }
  26.  
  27.   return sm;
  28. };
  29.  
  30. //
  31. // Print out a sparse matrix.
  32. //
  33.  
  34. print_sparse = function ( sm )
  35. {
  36.   for (i in members (sm))
  37.   {
  38.     for (j in members (sm.[i]))
  39.     {
  40.       printf (" row: %6s\tcol: %6s\tvalue: %g\n", i, j, sm.[i].[j]);
  41.     }
  42.   }
  43. };
  44.  
  45. //
  46. // Add two sparse matrices.
  47. //
  48.  
  49. //
  50. // Multiply two sparse matrices.
  51. //
  52.